package com.inossem.warehousestorage_inossem.activity.in_storage;

import android.app.Activity;
import android.content.Context;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.widget.ListView;
import android.widget.TextView;

import com.inossem.loadandrefresh.pullableview.PullableListView;
import com.inossem.utils.base.Utils;
import com.inossem.utils.listener.NoDoubleClickListener;
import com.inossem.warehousestorage_inossem.R;
import com.inossem.warehousestorage_inossem.activity.base.BaseActivity;
import com.inossem.warehousestorage_inossem.adapter.inStorage.ReceiveOperatorListAdapter;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageReceiveOverviewBean;
import com.inossem.warehousestorage_inossem.bean.inStorage.InStorageRoleBean;
import com.inossem.warehousestorage_inossem.constant.Constant;
import com.inossem.warehousestorage_inossem.listener.GsonBeanListener;
import com.inossem.warehousestorage_inossem.request.inStorage.InStorageGetRoleRequest;
import com.inossem.warehousestorage_inossem.utils.ActivityUtils;
import com.inossem.warehousestorage_inossem.utils.InStorageUtils;

import java.util.List;

public class InStorageReceiveAddOperatorActivity extends BaseActivity {

    private TextView title, nextStep;

//    private PullToRefreshLayout refresh;

    private PullableListView operatorList;

    private ReceiveOperatorListAdapter adapter;

    private List<InStorageRoleBean.DataBean.RoleListBean> mRoleList;

    private String mRoleName, mRoleCode;

    @Override
    protected void onCreateImpl(Bundle savedInstanceState) {
        setContentView(R.layout.activity_in_storage_receive_add_operator);
        initView(InStorageReceiveAddOperatorActivity.this);
    }

    @Override
    protected void onResumeImpl() {
        initData(InStorageReceiveAddOperatorActivity.this);
    }

    private void initView(Activity activity) {
        title = (TextView) activity.findViewById(R.id.headerTitle);
        nextStep = (TextView) activity.findViewById(R.id.nextStep);
//        refresh = (PullToRefreshLayout) activity.findViewById(R.id.operatorListRefresh);
        operatorList = (PullableListView) activity.findViewById(R.id.operatorList);
    }

    private void initData(Activity activity) {
        title.setText("入库经办人");
        nextStep.setOnClickListener(new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                if (InStorageUtils.hasReceiveOperator(activity)) {
                    Intent intent = new Intent(activity, InStorageReceiveConfirmActivity.class);
                    startActivity(intent);
                } else {
                    Utils.middleToast(activity, activity.getResources().getString(R.string.activity_receive_add_receiver_fill_in_receiver));
                }
            }
        });
        ActivityUtils.initBackAndFunction(activity, R.string.activity_receive_receive_item_title_short,     new NoDoubleClickListener() {
            @Override
            public void onNoDoubleClick(View v) {
                finish();
            }
        });

        operatorList.setPullUpFlag(false);
        operatorList.setPullDownFlag(false);
        getData(activity);
    }

    private void getData(Context context) {
        if (mRoleList == null) {
            InStorageGetRoleRequest.request(context, Constant.GET_ROLE_TYPE_IN_STORAGE_RECEIVE, new GsonBeanListener<InStorageRoleBean>() {
                @Override
                public void onSuccess(InStorageRoleBean bean) {
                    mRoleList = bean.getData().getRoleList();
                    InStorageReceiveOverviewBean.DataBean data = InStorageUtils.getInStorageReceiveOverviewBean(context).getData();
                    addReceiverListData(context, data, operatorList);
                }
            });
        } else {
            InStorageReceiveOverviewBean.DataBean data = InStorageUtils.getInStorageReceiveOverviewBean(context).getData();
            addReceiverListData(context, data, operatorList);
        }
    }

    private void addReceiverListData(Context context, InStorageReceiveOverviewBean.DataBean data, ListView listView) {
        adapter = new ReceiveOperatorListAdapter(context, data, mRoleList, mRoleName, mRoleCode, new InStorageReceiveAddOperatorListener() {
            @Override
            public void onDeleteOperatorListener() {
                initData(InStorageReceiveAddOperatorActivity.this);
            }

            @Override
            public void onSelectRole(String roleName, String roleCode) {
                mRoleName = roleName;
                mRoleCode = roleCode;
            }
        });
        listView.setAdapter(adapter);
    }

    public interface InStorageReceiveAddOperatorListener {
        void onDeleteOperatorListener();

        void onSelectRole(String roleName, String roleCode);
    }

}
